﻿@page "/times"
@page "/gamepublishtimelist"

@inject IHttpService _httpService

@inject IJSRuntime JS
@inject IDataCacheService _dataCacheService
@inject NavigationManager NavigationManager

<CnGalWebSite.Shared.MasaComponent.Shared.Cards.TitleTip Title="游戏发售时间汇总" Description="可以查看每月发售游戏列表"/>

<style>
    .row {
        flex: 0;
    }
</style>
<div>
    @if (isReady == false)
    {
        <CnGalWebSite.Components.Progressings.ProgressingCard Page />
    }
    else
    {
        <MDCard Class="ps-4 pe-4 mb-4">
            <div style="display: flex;flex-direction: column;align-items: center;align-content: center;" class="pt-4">
                <div class="title-large mb-3">游戏发售时间汇总</div>
                <div>
                    <CnGalWebSite.Components.Buttons.MasaButton Text="切换模式" Icon="@IconType.Switch.ToIconString()" TextStyle OnClick="OnSwitch" />
                </div>
            </div>
            <div style="display: flex; align-items: center; justify-content: center;">
                @if (Type == PublishGamesDisplayType.CardList)
                {
                    <CnGalWebSite.Components.Buttons.MasaButton Text="上个月" Icon="mdi-arrow-left-thick" TextStyle OnClick="OnClickLeft" />
                    <div style="max-width:170px" class="ps-2 pe-2">
                        <CnGalWebSite.Shared.MasaComponent.Shared.Components.MasaMonthPick Date="Date" DateChanged="DateChanged" />
                    </div>
                    <CnGalWebSite.Components.Buttons.MasaButton Text="下个月" Icon="mdi-arrow-right-thick" TextStyle OnClick="OnClickRight" />
                }
                else
                {
                    <div style="max-width: 180px" class="mb-4">
                        <CnGalWebSite.Components.Inputs.MasaDatePicker ShowTime="false" Value="After" ValueChanged="AfterChanged" />

                    </div>
                    <span class="ms-4 me-4">  ~  </span>
                    <div style="max-width: 180px" class="mb-4">
                        <CnGalWebSite.Components.Inputs.MasaDatePicker ShowTime="false" Value="Before" ValueChanged="BeforeChanged" />

                    </div>
                }
            </div>
        </MDCard>
        @if (Type == PublishGamesDisplayType.CardList)
        {
            <div>
                @foreach (var item in CardModel.OrderBy(s => s.PublishTime))
                {
                    <div class="mb-4">
                        <CnGalWebSite.Shared.MasaComponent.Shared.Cards.Search.EntryInforViewTip Model="item" ForcedShowPublishTime ShowType="false" />
                    </div>
                }
            </div>
        }
        else
        {
            if (TimelineModel.Any())
            {
                var i = 0;

                <div class="rounded shadow-sm bg-opacity p-3">
                    <MTimeline Dense="_dataCacheService.IsApp||TimelineModel.Count<3">

                        @foreach (var item in TimelineModel.OrderByDescending(s => s.PublishTime))
                        {
                            <MTimelineItem Large Color="@GetColor(i)">
                                <OppositeContent>
                                    <span>@(string.IsNullOrWhiteSpace(item.PublishTimeNote) ? item.PublishTime.Value.ToString("yyyy年M月d日") : item.PublishTimeNote)</span>
                                </OppositeContent>
                                <ChildContent>
                                    <MCard Class="elevation-2" @onclick="@(() => ViewAt(item.Id))">
                                        <CnGalWebSite.Shared.MasaComponent.Shared.Cards.Search.EntryInforViewTip Model="item" ShowType="false" Outline ShowDetailNumber="(_dataCacheService.IsApp||TimelineModel.Count<3)" BigCardStyle="@_dataCacheService.IsApp" AllowNav="false" />
                                    </MCard>
                                </ChildContent>
                            </MTimelineItem>


                            i = (i + 1) % 11;

                        }
                    </MTimeline>
                </div>

            }

        }


        @if (isFirst == false && ((Type == PublishGamesDisplayType.CardList && CardModel.Any() == false) || (Type == PublishGamesDisplayType.Timeline && TimelineModel.Any() == false)))
        {
            <NotFoundCard  />
        }
    }
</div>

@code {
    [CascadingParameter]
    public ErrorHandler ErrorHandler { get; set; }

    [SupplyParameterFromQuery]
    [Parameter]
    public int Year { get; set; }

    [SupplyParameterFromQuery]
    [Parameter]
    public int Month { get; set; }

    [SupplyParameterFromQuery]
    [Parameter]
    public long BeforeTime { get; set; }

    [SupplyParameterFromQuery]
    [Parameter]
    public long AfterTime { get; set; }

    [SupplyParameterFromQuery]
    [Parameter]
    public int type { get; set; }

    public PublishGamesDisplayType Type { get; set; }

    DateOnly Date = DateOnly.FromDateTime(DateTime.Now);

    DateTime Before = DateTime.Now.AddDays(1);

    DateTime After = DateTime.Now.AddMonths(-1);

    bool isFirst = true;

    List<EntryInforTipViewModel> CardModel = new List<EntryInforTipViewModel>();
    List<PublishGamesTimelineModel> TimelineModel = new List<PublishGamesTimelineModel>();

    bool isReady = false;

    public string GetColor(int index)
    {
        return index switch { 0 => "red", 1 => "pink", 2 => "purple", 3 => "deep-purple", 4 => "indigo", 5 => "blue", 6 => "cyan", 7 => "teal", 8 => "green", 9 => "yellow", 10 => "orange", _ => "black" };
    }

    protected override async Task OnParametersSetAsync()
    {

        try
        {
            isFirst = true;
            Type = (PublishGamesDisplayType)type;

            if (Type == PublishGamesDisplayType.CardList)
            {
                if (Year == 0 || Month == 0 || Month > 12)
                {
                    Date = DateOnly.FromDateTime(DateTime.Now);
                }
                else
                {
                    Date = new DateOnly(Year, Month, 1);
                }

                CardModel = await _dataCacheService.PublishGameTimesDataCatche.GetCache($"?year={Year}&month={Month}");

            }
            else
            {
                if (AfterTime != 0)
                {
                    After = AfterTime.ToString().TransTime();
                }
                if (BeforeTime != 0)
                {
                    Before = BeforeTime.ToString().TransTime();
                }

                TimelineModel = await _dataCacheService.PublishGamesTimelineDataCatche.GetCache($"?beforeTime={BeforeTime}&aftertime={AfterTime}");
            }
            isFirst = false;
            isReady = true;

        }
        catch (Exception ex)
        {
            await ErrorHandler.ProcessError(ex, "获取游戏发售时间列表失败");
        }

    }


    protected override async Task OnInitializedAsync()
    {

    }

    public void OnClickLeft()
    {
        DateChanged(Date.AddMonths(-1));
        
    }

    public void OnClickRight()
    {
        DateChanged(Date.AddMonths(1));
        
    }

    public void DateChanged(DateOnly date)
    {
        Date = date;
        Refresh();
    }

    public void BeforeChanged(DateTime date)
    {
        Before = date;
        Refresh();
    }

    public void AfterChanged(DateTime date)
    {
        After = date;
        Refresh();
    }

    public void OnSwitch()
    {
        if (Type == PublishGamesDisplayType.CardList)
        {
            Type = PublishGamesDisplayType.Timeline;
        }
        else
        {
            Type = PublishGamesDisplayType.CardList;
        }
        isFirst = false;
        Refresh();
    }

    public void ViewAt(int id)
    {
        NavigationManager.NavigateTo("/entries/index/" + id);
        
    }

    public void Refresh()
    {
        try
        {
            if (Type == PublishGamesDisplayType.CardList)
            {
                NavigationManager.NavigateTo(NavigationManager.GetUriWithQueryParameters(new Dictionary<string, object>
                {
                    ["year"] = Date.Year,
                    ["month"] = Date.Month,
                    ["type"] = (int)Type,
                }));
            }
            else
            {
                NavigationManager.NavigateTo(NavigationManager.GetUriWithQueryParameters(new Dictionary<string, object>
                {
                    ["beforeTime"] = Before.ToUnixTimeMilliseconds(),
                    ["afterTime"] = After.ToUnixTimeMilliseconds(),
                    ["type"] = (int)Type,
                }));
            }


        }
        catch
        {

        }

        
    }
}
